package com.ruoyi.personnel.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.personnel.domain.ReserveInformation;
import com.ruoyi.personnel.enums.VisitStatus;
import com.ruoyi.personnel.mapper.ReserveInformationMapper;
import com.ruoyi.personnel.service.IReserveInformationService;
import com.ruoyi.personnel.util.StringToData;
import com.ruoyi.personnel.vo.OpenIdVO;
import com.ruoyi.personnel.vo.ReserveInformationVO;
import com.ruoyi.personnel.vo.StatusAndOpenIdVO;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 预约信息Service业务层处理
 *
 * @author hyj
 * @date 2020-02-28
 */
@Service
public class ReserveInformationServiceImpl implements IReserveInformationService {

    @Autowired
    private ReserveInformationMapper reserveInformationMapper;

    @Autowired
    private ISysConfigService configService;

    private String phoneGetAccessName(String phone){
        return reserveInformationMapper.accessNameByPhone(phone);
    }

    /**
     * 新增预约信息
     * @param reserveInformationVO
     * @return 是否成功插入
     */

    @Override
    public int saveReserveInformationService(ReserveInformationVO reserveInformationVO) {
        int count = 0;
        ReserveInformation reserveInformation = new ReserveInformation();
        BeanUtils.copyBeanProp(reserveInformation,reserveInformationVO);//将VO对象赋值到实体类中
        reserveInformation.setVisitTime(StringToData.toData(reserveInformationVO.getVisitTime()));
        //补全状态
        reserveInformation.setStatus(VisitStatus.APPOINTMENT.getCode());
        //补全创建时间，修改人，修改时间
        reserveInformation.setCreateTime(DateUtils.getNowDate());//创建记录时间
        reserveInformation.setModifyTime(DateUtils.getNowDate());//修改时间
        reserveInformation.setModifier(reserveInformationVO.getOpenId());
        count = reserveInformationMapper.createReserveInformation(reserveInformation);
        return count;
    }

    /**
     * 通过ID查询是否超过预约时间
     * @param id 预约id
     * @return
     */
    @Override
    public AjaxResult selectReserveInformationByIdToAjaxResult(Long id) {
        ReserveInformation reserveInformation = reserveInformationMapper.selectReserveInformationById(id);
        if(reserveInformation==null){
            return new AjaxResult(AjaxResult.Type.SUCCESS, "该记录不存在", "");
        }
        Date visitTime = reserveInformation.getVisitTime();
        Long oldTime = visitTime.getTime();//预约时间

        Date nowDate = DateUtils.getNowDate();
        Long nowTime = nowDate.getTime();//当前时间

        long oneHour  = 1000 * 60 * 60;
        Long diff = (oldTime - nowTime) / oneHour;//时间差
        Long hour = Long.parseLong(configService.selectConfigByKey("personnel.access.day"));//默认被超过时间
        if(hour<diff){//规定时间<
            return new AjaxResult(AjaxResult.Type.SUCCESS, "预约成功", "");
        }else{
            return new AjaxResult(AjaxResult.Type.SUCCESS, "该预约已过期", "");
        }
    }
    /**
     * 查询预约信息
     *
     * @param id 预约信息ID
     * @return 预约信息
     */
    @Override
    public ReserveInformation selectReserveInformationById(Long id)
    {
        return reserveInformationMapper.selectReserveInformationById(id);
    }



    /**
     * 查询预约信息列表
     *
     * @param reserveInformation 预约信息
     * @return 预约信息
     */
    @Override
    public List<ReserveInformation> selectReserveInformationList(ReserveInformation reserveInformation)
    {
        return reserveInformationMapper.selectReserveInformationList(reserveInformation);
    }

    /**
     * 根据openId查询预约信息列表
     * @param statusAndOpenIdVO
     * @return
     */
    @Override
    public List<ReserveInformationVO> selectReserveInformationList(StatusAndOpenIdVO statusAndOpenIdVO)
    {
        ReserveInformation reserveInformation = new ReserveInformation();
        BeanUtils.copyBeanProp(reserveInformation,statusAndOpenIdVO);//将对象赋值到实体类中
        List<ReserveInformation> reserveInformations = reserveInformationMapper.selectReserveInformationList(reserveInformation);//取到该openId下的所有预约信息
        List<ReserveInformationVO> reserveInformationVOs = new ArrayList<>();//VO对象列表
        for( ReserveInformation reserveInformation2:reserveInformations){
            ReserveInformationVO reserveInformationVO = new ReserveInformationVO();
            BeanUtils.copyBeanProp(reserveInformationVO,reserveInformation2);//将预约信息撞到VO对象中
            Date date = reserveInformation2.getVisitTime();//获取访问时间
            reserveInformationVO.setOutVisitTime(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm", date));
            String accessPhone = reserveInformationVO.getAccessPhone();      //获取受访人手机号码
            String accessName = phoneGetAccessName(accessPhone);             //获取受访人姓名
            reserveInformationVO.setAccessName(accessName);                  //补全受访人姓名
            reserveInformationVOs.add(reserveInformationVO);                 //将VO对象添加到VO对象列表中
        }
        return reserveInformationVOs;
    }
    /**
     * 新增预约信息
     *
     * @param reserveInformation 预约信息
     * @return 结果
     */
    @Override
    public int insertReserveInformation(ReserveInformation reserveInformation)
    {
        reserveInformation.setCreateTime(DateUtils.getNowDate());
        return reserveInformationMapper.createReserveInformation(reserveInformation);
    }

    /**
     * 修改预约信息
     *
     * @param reserveInformation 预约信息
     * @return 结果
     */
    @Override
    public int updateReserveInformation(ReserveInformation reserveInformation)
    {
        return reserveInformationMapper.updateReserveInformation(reserveInformation);
    }
    @Override
    public int updateReserveInformation(StatusAndOpenIdVO StatusAndOpenIdVO)
    {
        ReserveInformation reserveInformation = new ReserveInformation();
        BeanUtils.copyBeanProp(reserveInformation,StatusAndOpenIdVO);
        return reserveInformationMapper.updateReserveInformation(reserveInformation);
    }

    /**
     * 修改预约信息
     * @param reserveInformationVO 预约信息VO对象
     * @return 结果
     */
    @Override
    public int updateReserveInformation(ReserveInformationVO reserveInformationVO)
    {
        ReserveInformation reserveInformation = new ReserveInformation();
        BeanUtils.copyBeanProp(reserveInformation,reserveInformationVO);
        reserveInformation.setVisitTime(StringToData.toData(reserveInformationVO.getVisitTime()));
        reserveInformation.setModifyTime(DateUtils.getNowDate());//补全修改时间
        reserveInformation.setModifier(reserveInformation.getOpenId());//补全最后修改人
        return reserveInformationMapper.updateReserveInformation(reserveInformation);
    }

    /**
     * 删除预约信息对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteReserveInformationByIds(String ids)
    {
        return reserveInformationMapper.deleteReserveInformationByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除预约信息信息
     *
     * @param id 预约信息ID
     * @return 结果
     */
    @Override
    public int deleteReserveInformationById(Long id)
    {
        return reserveInformationMapper.deleteReserveInformationById(id);
    }

    /**
     * 根据openId查询访客（预约）记录
     * @param openId
     * @return
     */
    @Override
    public List<ReserveInformation> selectExamineByOpenId(String openId) {
        return reserveInformationMapper.selectExamineByOpenId(openId);
    }

}
